<?php

// Namespace

namespace Loop;


/**
 * @package		Loop
 * @category	Helper
 */

class CDocumentationHelper
{
	/**
	 * Generate the html for the APIs
	 *
	 * @param array 	$result 	The content for the html file, pass by reference	 	 
	 */

	public function generateHtmlAPI($result, $buttonTest = true)
	{
		// Initialize variable

		$content = '';


		// Is there result ?

		if 
		(
			isset($result['API']) && 
			count($result['API']) > 0
		)
		{
			
			$content.=
			'<div>
				<table class="table table-hover table-bordered">
					<thead>
					    <tr>
					      	<th>API Name</th>
					      	<th>Description</th>
					     	<th>Arguments</th>
					      	<th>Return</th>
					      	<th>Rich Documentation</th>
					    </tr>
					</thead>
					<tbody>';

			foreach ($result['API'] as $methodName => $methodInfos)
			{

				$content .= '<tr>';


				// NAME

				$content .= '<td>';
				if ($buttonTest == true)
				{
					$content .=	'<a href="' .  url('[Loop]/page/admin/documentation/richDocumentation/?extensionId=' . $result['extensionId'] . '&concept=' . $result['directory'] . '&file=' . $result['classNameWithoutNamespace'] . '&method=' . $methodName) . '">
									' . substr($methodName, 4) . '
									</a>';
					$content .= '</br>';					
					$content .= '<a href="' . url('[Loop]/page/admin/sandbox/api') 
									. '?extensionId=' . $result['extensionId'] 
									.'&directory=' . $result['directory'] 
									. '&fileName=' . $result['classNameWithoutNamespace'] 
									. '&api=' . $methodName . '">';	
					$content .= '<i>Test it</i>';
					$content .= '</a>';
				}
				else
				{
					$content .=  substr($methodName, 4) ;
				}

				$content .= '</td>';


				// DESCRIPTION

				$content .= '<td>';

				// Is there a method description ?

				if (empty($methodInfos['description']['infos']) === false)
				{
					$content .= $methodInfos['description']['infos'];	
				}

				$content .= '</td>';

				
				// PARAMETERS

				$content .= '<td>';

				// Is there parameters ?

				if 
				(
					isset($methodInfos['description']['params']) === true &&
					count($methodInfos['description']['params']) > 0
				)
				{

					// Order the arguments by isRequired

					$arguments = app()->getArrayHelper()->orderByKey($methodInfos['description']['params'], 'isRequired', SORT_DESC, true);

					foreach ($arguments	as $paramName => $paramDetail) 
					{

						$content .= '<b>' . $paramName . '</b>';


						$content .='<ul>';


						// Is there a description ?

						if (isset($paramDetail['description']) === true)
						{
							$content .= '<li>Description : ' . $paramDetail['description'] . '</li>';
						}	

						// Is there a type ?

						if (isset($paramDetail['type']) === true)
						{
							$content .= '<li>Type : ' . $paramDetail['type'] . '</li>';
						}		

						// Is this require ? ?

						if (isset($paramDetail['isRequired']) === true && $paramDetail['isRequired'] == 1)
						{
							$content .= '<li>Is required</li>';
						}	

						// Is there a default value ?

						if (isset($paramDetail['defaultValue']) === true)
						{
							$content .= '<li>Default Value : ' . $paramDetail['defaultValue'] . '</li>';;
						}							

						$content .='</ul>';
					}

				}

				$content .= '</td>';
				

				// RETURN

				$content .= '<td>';

				// Is there a param return ?

				if (empty($methodInfos['description']['return']) === false)
				{

					// Is there a description ?

					if (isset($methodInfos['description']['return']['description']) === true)
					{
						$content .= '<b>Description : </b>' . $methodInfos['description']['return']['description'];
						$content .= '<br />';
					}	

					// Is there a type ?

					if (isset($methodInfos['description']['return']['type']) === true)
					{
						$content .= '<b>Type : </b>' . $methodInfos['description']['return']['type'];
					}				

				}

				$content .= '</td>';


				// Rich doc

				$content .= '<td>';

				// Is there a method description ?

				if (empty($methodInfos['description']['richDocumentation']) === false)
				{
					$content .= $methodInfos['description']['richDocumentation']['content'];	
				}

				$content .= '</td>';				
			}

			$content .= '</tbody></table></div>';
		}

		return $content;
	}


	/**
	 * Generate the html for the Input/Output
	 *
	 * @param array 	$result 	The content for the html file, pass by reference	 	 
	 */

	public function generateHtmlIO($result)
	{
		// Initialize variables

		$content = '';

		$content .= '<div>
				<table class="table table-hover table-bordered">
					<thead>
						<tr>
							<th>Name</th>
							<th>Description</th>
							<th>Instances</th>
						</tr>
					</thead>
					<tbody>';

		foreach ($result['ioProperties'] as $propertyName => $propertyDetails)
		{

			$content .=
						'<tr>
							<td>' . $propertyName . '</td>
							<td>';

								foreach ($propertyDetails as $name => $details)
								{
									// Is it type object ?

									if
									(
										$name !== 'object' &&
										$name !== 'instances' &&
										empty($details) === false
									)
									{
										if (is_array($details))
										{
											$details = json_encode($details);
										}

										$content .= '<li>' . $name .': ' . $details . '</li>';				
									}
								}
							$content .= '</td>
							<td>' . $propertyDetails['instances'] . '</td>
						</tr>';
							}

					$content .= '</tbody>
				</table>
			</div>';

		return $content;
	}


	/**
	 * Generate the html for the functions found in the model/class
	 *
	 * @param string 	$methodType 	The type of method : static/public/private/protected		 
	 * @param array 	$result 		The content for the html file, pass by reference	 	 
	 */

	public function generateHtmlMethod($methodType, &$result)
	{
		// Initialize variables

		$content = '';
		$type = $methodType . 'Functions';


		// Is there result ?

		if (count($result[$type]) > 0)
		{
			// Name of the method

			$content .= '<h4 style="text-align: center;">' . ucfirst($methodType) . ' methods</h4>' ;

			$content.=
			'<div>
				<table class="table table-hover table-bordered">
					<thead>
					    <tr>
					      <th>Method Name</th>
					      <th>Description</th>
					       <th>Arguments</th>
					      <th>Return</th>
					      <th>Rich Documentation</th>
					    </tr>
					</thead>
					<tbody>';


			//

			foreach ($result[$type] as $methodName => $methodInfos)
			{
				$content .='<tr>';
				$content .= '<td>
								<a href="' .  url('[Loop]/page/admin/documentation/richDocumentation/?extensionId=' . $result['extensionId'] . '&concept=' . $result['directory'] . '&file=' . $result['classNameWithoutNamespace'] . '&method=' . $methodName) . '">
								' . $methodName . '
								</a>
							</td>';


				// DESCRIPTION

				$content .= '<td>';

				// Is there a method description ?

				if (empty($methodInfos['description']['infos']) === false)
				{
					$content .= $methodInfos['description']['infos'];	
				}

				$content .= '</td>';


				// PARAMETERS

				$content .= '<td>';

				// Is there parameters ?

				if (count($methodInfos['params']) > 0)
				{

					foreach ($methodInfos['params']	as $param) 
					{

						$content .= '<b>$' . $param->name . '</b>';


						$content .='<ul>';

						if ($param->isOptional()) 
						{
							$defaultValue = $param->getDefaultValue();

							if (is_null($defaultValue) === true)
							{
								$defaultValue = 'NULL';
							}
							if (is_array($defaultValue) === true)
							{
								$defaultValue = 'array';							}

							$content .= '<li>Default : ' . $defaultValue . '</li>';
						}


						// Is there a comment for the param ?

						if (isset($methodInfos['description']['params']['$' . $param->name]) === true)
						{
							// Is there a description ?

							if (isset($methodInfos['description']['params']['$' . $param->name]['description']) === true)
							{
								$content .= '<li>Description : ' . $methodInfos['description']['params']['$' . $param->name]['description'] . '</li>';
							}	

							// Is there a type ?

							if (isset($methodInfos['description']['params']['$' . $param->name]['type']) === true)
							{
								$content .= '<li>Type : ' . $methodInfos['description']['params']['$' . $param->name]['type'] . '</li>';
							}				
						}
						$content .='</ul>';
					}
				}

				$content .= '</td>';


				// RETURN

				$content .= '<td>';

				// Is there a param return ?

				if (empty($methodInfos['description']['return']) === false)
				{


					// Is there a description ?

					if (isset($methodInfos['description']['return']['description']) === true)
					{
						$content .= '<b>Description : </b>' . $methodInfos['description']['return']['description'];
					}	

					// Is there a type ?

					if (isset($methodInfos['description']['return']['type']) === true)
					{
						$content .= '<p><b>Type : </b>' . $methodInfos['description']['return']['type'] . '</p>';
					}				
				}

				$content .= '</td>';


				// Rich doc

				$content .= '<td>';

				// Is there a method description ?

				if (empty($methodInfos['description']['richDocumentation']) === false)
				{
					$content .= $methodInfos['description']['richDocumentation']['content'];	
				}

				$content .= '</td>';					

			}


			$content .= '</tbody></table></div>';	
		}

		return $content;
	}


	/**
	 * Generate the html for the properties found in the model/class
	 *
	 * @param string 	$methodType 	The type of method : static/public/private/protected		 
	 * @param array 	$result 	The content for the html file	 	 
	 */

	public function generateHtmlProperties($methodType, &$result)
	{
		// Initialize variables

		$content = '';
		$type = $methodType . 'Properties';


		// Is there result ?

		if (count($result[$type]) > 0)
		{

		// Name of the function

			$content .= '<h4 style="text-align: center;">' . ucfirst($methodType) . '</h4>' ;

			$content.=
			'<div>
				<table class="table table-hover table-bordered">
					<thead>
					    <tr>
					      <th>Property Name</th>
					      <th>Default Value</th>
					      <th>Description</th>
					    </tr>
					</thead>
					<tbody>';



			// Create the class

			$reflectionClass = new \ReflectionClass($result['className']);


			// For each property found

			foreach ($result[$type] as $propertyName => $details)
			{
				$content .= '<tr>';
				$content .= '<td>$' . $propertyName .'</td>';
				if ($result['className'] != 'Loop\CApplication')			
				{
					// Get the property

					$defaultProperties = $reflectionClass->getDefaultProperties();

					if(array_key_exists($propertyName, $defaultProperties) === true)
					{
						$defaultValue = $defaultProperties[$propertyName];


						if (is_null($defaultValue))
						{
							$defaultValue = 'NULL';
						}

						$content .= '<td>' . $defaultValue . '</td>';	

						$content .= '<td>';

						// Is there a comment ?

						if
						(
							isset($details['description']) === true &&
							empty($details['description']) === false
						)
						{
							$content .= $details['description'];	
						}

						$content .= '</td>';
						$content .= '</tr>';
					}
				}	
			}

			$content .= '</tbody></table></div>';	
		}


		return $content;
	}
}
?>